package com.yksj.healthtalk.ui.salon;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.adapter.SalonInComeDetailAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.SalonIncomeDetailEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.JsonParseUtils;

public class SalonIncomeDetailActivity extends BaseFragmentActivity implements OnClickListener{
	
	private Button mTitleBackBtn;
	private Button mTitleSureBtn;
	private TextView mTitleName;
	private ListView mListView;
	private ArrayList<SalonIncomeDetailEntity> incomeDetailEntities;
	private SalonInComeDetailAdapter adapter;
	private TextView mDateTextView;//日期
	private TextView mMoneyTextView;//日期
	private Button pageUpBtn;
	private Button pageDownBtn;
	private String date;
	
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.salon_income_detail);
		initWidget();
		initData();
	}

	private void initData() {
		incomeDetailEntities = new ArrayList<SalonIncomeDetailEntity>();
		adapter = new SalonInComeDetailAdapter(this, incomeDetailEntities);
		mTitleName.setText("收入详情");
		mListView.setAdapter(adapter);
		
		//查看记录详情
		if (getIntent().hasExtra("date")) {
			date = getIntent().getExtras().getString("date");
			getIncomeDetailInfomation(SmartFoxClient.getSmartFoxClient().getUserId(),date );
		}
	
	}

	private void initWidget() {
		// TODO Auto-generated method stub
		mTitleBackBtn = (Button) findViewById(R.id.title_back);
		mTitleSureBtn = (Button)findViewById(R.id.title_right);
		mTitleName = (TextView)findViewById(R.id.title_lable);
		mListView = (ListView)findViewById(R.id.list);
		mDateTextView = (TextView)findViewById(R.id.date);
		mMoneyTextView = (TextView)findViewById(R.id.money);	
		pageDownBtn = (Button)findViewById(R.id.page_down);
		pageUpBtn = (Button)findViewById(R.id.page_up);
		
		mTitleBackBtn.setOnClickListener(this);
		pageDownBtn.setOnClickListener(this);
		pageUpBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODsO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			
			finish();
			break;
		case R.id.page_down:
			date = String.valueOf(Integer.valueOf(date)+1); 
			getIncomeDetailInfomation(SmartFoxClient.getSmartFoxClient().getUserId(),date );
			break;
		case R.id.page_up:
			date = String.valueOf(Integer.valueOf(date)-1); 
			getIncomeDetailInfomation(SmartFoxClient.getSmartFoxClient().getUserId(),date );
			break;

		default:
			break;
		}
	}

	/**
	 * 获取沙龙详细信息(精确到天)
	 * @param customerId
	 * @param time
	 */
	public void getIncomeDetailInfomation(String customerId, String time){
		incomeDetailEntities.clear();
		HttpRestClient.doHttpFindInComeDetailData(customerId, time, new AsyncHttpResponseHandler(this){
			
			@Override
			public void onSuccess(int statusCode, String content) {
				incomeDetailEntities.addAll(JsonParseUtils.getSalonIncomeDetail(content));
				adapter.notifyDataSetChanged();
				adapter.setMoney(0);
				mMoneyTextView.setText(String.format(getString(R.string.money), adapter.getMoney()));
				if (date != null) {
					mDateTextView.setText(date+"总额");
				}
				super.onSuccess(statusCode, content);
			}
		});
	}

}
